package com.huiminpay.merchant.controller;

import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.domain.PageVO;
import com.huiminpay.common.cache.util.QRCodeUtil;
import com.huiminpay.merchant.api.IMerchantService;
import com.huiminpay.merchant.common.util.SecurityUtil;
import com.huiminpay.merchant.dto.MerchantDTO;
import com.huiminpay.merchant.dto.StoreDTO;
import com.huiminpay.transaction.api.dto.QRCodeDto;
import com.huiminpay.transaction.api.service.TransactionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Api(value = "商户平台‐门店管理", tags = "商户平台‐门店管理", description = "商户平台‐门店的增删改查")
@RestController
@Slf4j
public class StoreController {

    @Reference
    private IMerchantService merchantService;
    @Reference
    TransactionService transactionService;
    /*
     * 门店二维码订单标题
     *  */
    @Value("${huiminpay.c2b.subject}")
    private String subject;

    @Value("${huiminpay.c2b.body}")
    private String body;


    @ApiOperation("分页条件查询商户下门店")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "int",
                    paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true, dataType =
                    "int", paramType = "query")})
    @PostMapping("/my/stores/merchants/page")
    public PageVO<StoreDTO> queryStoreByPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {

        Long merchantId = SecurityUtil.getMerchantId();
        StoreDTO storeDTO = new StoreDTO();
        storeDTO.setMerchantId(merchantId);
        // 分页查询
        PageVO<StoreDTO> storeDTOS = merchantService.queryStoreByPage(storeDTO, pageNo, pageSize);
        System.out.println(storeDTOS);
        return storeDTOS;
    }

    @ApiOperation("生成商户应用门店二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "商户应用id", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "storeId", value = "商户门店id", required = true, dataType = "String", paramType = "path"),
    })
    @GetMapping(value = "/my/apps/{appId}/stores/{storeId}/app-store-qrcode")
    public String createCScanBStoreQRCode(@PathVariable String appId, @PathVariable Long storeId) throws BusinessException {
        // 商户Id
        Long merchantId = SecurityUtil.getMerchantId();
        // 商户信息
        MerchantDTO merchantDTO = merchantService.queryMerchantById(merchantId);

        // 生成二维码链接
        QRCodeDto qrCodeDto = new QRCodeDto();
        qrCodeDto.setMerchantId(merchantId);
        qrCodeDto.setAppId(appId);
        qrCodeDto.setStoreId(storeId);

        // 标题 用商户名称替换%s
        String subjectFormat = String.format(subject, merchantDTO.getMerchantName());
        qrCodeDto.setSubject(subjectFormat);

        // 内容，格式："向%s 付款"
        String bodyFormat = String.format(body, merchantDTO.getMerchantName());

        qrCodeDto.setBody(bodyFormat);

        // 获取二维码的URL
        String storeQRCodeUrl = transactionService.createStoreQRCode(qrCodeDto);
        log.info("[merchantId:{},appId:{},storeId:{}]createCScanBStoreQRCode is {}", merchantId, appId, storeId, storeQRCodeUrl);
        QRCodeUtil qrCodeUtil = new QRCodeUtil();
        try {
            String qrCode = qrCodeUtil.createQRCode(storeQRCodeUrl, 200, 200);
            return qrCode;
        } catch (IOException e) {
            throw new BusinessException(CommonErrorCode.E_200007);
        }
    }

}
